#include <algorithm>
#include <cmath>
#include <cstdio>
#define N 100010
using namespace std;
int d[N], color[N];
int fa[N][32];
int n, q;
int main() {
	freopen("position.in", "r", stdin);
	freopen("position.out", "w", stdout);
	scanf("%d %d", &n, &q);
	int tmp = 0, tmp1 = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d %d", &tmp, &d[i]);
		color[tmp] = i;
	}
	for (int i = 1; i <= n; ++i) {
		fa[i][0] = color[d[i]];
	}
	for (int i = 1; i < 32; ++i) {
		for (int j = 1; j <= n; ++j) {
			fa[j][i] = fa[fa[j][i - 1]][i - 1];
		}
	}
	for (int j = 0; j < q; ++j) {
		scanf("%d %d", &tmp, &tmp1);
		for (int i = 31; ~i; --i) {
			if (pow(2, i) <= tmp1) {
				tmp = fa[tmp][i];
				tmp1 -= pow(2,i);
			}
		}
		printf("%d\n", tmp);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}